<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .min {
      position: relative;
      width: 400px;
      height: 330px;
      margin-top: 10px;
      border: 1px #ccc solid;

    }

    .ps {
      display: flex;
      flex-wrap: wrap;
      height: 260px;
      overflow: auto;
    }

    .ps p {
      margin-left: 10px;
      background-color: #ccc;
      border-radius: 25%;
      cursor: pointer;
    }

    .select {
      background-color: yellow !important;
    }

    input.el-input__inner {
      text-align: center;
    }
  </style>
</head>

<body>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <!-- 引入组件库 -->
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <div id="app">
    <el-input v-model="val" style="width: 200px;" :clearable="false" size="small" @focus="handleFoucus" readonly>
    </el-input>
    <div class="min" v-if="showNnum">
      <p style="text-align: center;"> <span style="padding-right: 120px;  cursor: pointer;" @click="handlePre">《</span>
        选值 <span style="padding-left: 120px;  cursor: pointer;" @click="handleNext">》</span></p>
      <div class="ps">
        <p v-for="(item, index) in minData" :key="index" @click="handleSelct(item)"
          :class="item.select ? 'select' : ''">{{item.num}}</p>
      </div>
    </div>
  </div>
  <script>

    var app = new Vue({
      el: '#app',
      data: {
        val: '',
        minData: [],
        maxData: [],
        showNnum: false,
        bool: false,
        selectData: []
      },
      created () {

      },
      mounted () {
        this.init()


        let body = document.querySelector('body')
        console.log('body: ', body);
        body.addEventListener('click', function () {
          console.log(1111111);
              this.showNnum = false
        })
      },
      watch: {
        showNnum (val) {
          if (!val) {
            this.init()
          }
        }
      },
      methods: {
        handleFoucus () {
          if (this.val) {
            this.showSelect()

          }

          // if (index !== -1) {
          //   this.minData.map(item => {
          //     if (item.num == this.val.slice(0, index) || item.num == this.val.slice(index + 1)) {
          //       item.select = true
          //     }
          //     this.bool = true
          //   })

          // }

          this.showNnum = true
        },

        showSelect () {

          let num1 = ''
          let num2 = ''
          console.log('this.selectData: ', this.selectData);
          if (this.selectData.length === 1) {
            num1 = this.selectData[0].num
          }

          if (this.selectData.length === 2) {
            num1 = this.selectData[0].num
            num2 = this.selectData[1].num
          }



          this.minData.map(item => {

            if (item.num === num1 || item.num === num2) {
              item.select = true
            }
          })
        },


        handleSelct (item) {
          if (this.selectData.length === 2) {
            this.selectData = []
          }
          if (this.bool || this.selectData.length === 2) {
            this.minData.map(item =>
              item.select = false
            )
            this.bool = false
          }



          let index = this.minData.findIndex(i => i.num === item.num)

          this.minData[index].select = true
          this.minData[index].time++



          // this.selectData = this.minData.filter(item => item.select === true)
          this.minData.filter(item => {
            if (item.select) {
              this.selectData.push(item)
            }
          })

          if (this.selectData.length > 2) {
            this.selectData.shift()
          }


          console.log('this.selectData: ', this.selectData);

          if (this.minData[index].time === 2) {
            this.selectData = [...this.selectData, ...this.selectData]
          }

          if (this.selectData.length === 1 && this.minData[index].time === 2) {
            this.val = this.selectData[0].num + '~' + this.selectData[0].num
          }

          if (this.selectData.length === 2) {
            this.val = this.selectData[0].num + '~' + this.selectData[1].num

          }

          if (this.selectData.length === 2 || this.minData[index].time === 2) {



            setTimeout(() => {
              this.showNnum = false
            }, 300)
          }
        },


        init () {
          this.minData = []
          for (i = 0; i < 100; i++) {
            this.minData.push({
              num: i,
              select: false,
              time: 0
            })
          }
        },



        handlePre () {

          let [first] = this.minData

          this.minData = []

          for (let i = first.num - 100; i < first.num; i++) {
            this.minData.push({
              code: i,
              num: i,
              select: false,
              time: 0
            })

          }
          this.showSelect()
        },
        handleNext () {
          let last = this.minData[this.minData.length - 1]

          this.minData = []

          for (let i = last.num + 1; i < last.num + 101; i++) {
            this.minData.push({
              num: i,
              select: false,
              time: 0
            })

          }
          this.showSelect()
        },


      }
    }
    )
  </script>


</body>

</html>